#coding:utf-8
class Foo(object):
    def __enter__(self):
        '''进入with语句的时候被with调用'''
        print('enter called')

    def __exit__(self, exc_type, exc_val, exc_tb):
        '''离开with语句的时候被with调用'''
        print('exit called')
        print('exc_type:%s' % exc_type)  # 错误类型
        print('exc_val:%s' % exc_val)  # 错误说明
        print('exc_tb:%s' % exc_tb)  # 错误追踪


with Foo() as foo:
    print('hello foo')
    a = 1 / 0  # 自定义异常
    print('hello end')